///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_MAT_DETAIL_EIGEN_WRAPPER_H_
#define _BUOLA_MAT_DETAIL_EIGEN_WRAPPER_H_

#include <buola/buola.h>
#include <buola/mat/cmat.h>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
#undef Success
#include <Eigen/Dense>
#pragma GCC diagnostic pop

namespace buola { namespace mat { namespace detail {

template<typename tType>
struct MEigenMapType
{
    typedef MMatrixTraits<ODecay<tType>> TTraits;

    static_assert(TTraits::cEigenMappable,"trying to eigen-map a non-mappable expression");
    
    typedef Eigen::Matrix<typename TTraits::TScalar,
                          (TTraits::cRows==cDynamic?Eigen::Dynamic:(int)TTraits::cRows),
                          (TTraits::cCols==cDynamic?Eigen::Dynamic:(int)TTraits::cCols),
                          ((TTraits::cRowLinearAccess&&TTraits::cCols!=1)?Eigen::RowMajor:Eigen::ColMajor)>
                                TEigenMatrixType;

    ///\todo check why aligned does not work
    typedef Eigen::Map<TEigenMatrixType> type;
    typedef Eigen::Map<const TEigenMatrixType> const_type;
};

template<typename tMat>
using OEigenMatrixType=typename MEigenMapType<tMat>::TEigenMatrixType;

template<typename tMat>
using OEigenMapType=typename MEigenMapType<tMat>::type;

template<typename tMat>
using OEigenConstMapType=typename MEigenMapType<tMat>::const_type;

template<typename tMat>
OEigenMapType<tMat> eigen_map(tMat &pMat)
{
    return OEigenMapType<tMat>(pMat.Data(),pMat.Rows(),pMat.Cols());
}
    
template<typename tMat>
OEigenConstMapType<tMat> eigen_map(const tMat &pMat)
{
    return OEigenConstMapType<tMat>(pMat.Data(),pMat.Rows(),pMat.Cols());
}

template<typename tMat>
OEnableIf<QEigenMappable<tMat>,const tMat&> eigen_mappable(const tMat &pMat)
{
    return pMat;
}

template<typename tMat>
OEnableIfNot<QEigenMappable<tMat>,OMatrixType<tMat>> eigen_mappable(const tMat &pMat)
{
    return pMat;
}

/*namespace detail*/ } /*namespace mat*/ } /*namespace buola*/ }

#endif
